﻿using FirstFloor.ModernUI.Windows.Controls;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Vtron.LEDCorrective.EDSDKWrapper;

namespace Vtron.LEDCorrective.Pages
{
    /// <summary>
    /// CameraConnect.xaml 的交互逻辑
    /// </summary>
    public partial class CameraConnect : UserControl
    {
        public CameraConnect()
        {
            InitializeComponent();

            Loaded += CameraConnect_Loaded;
        }

        List<Camera> CamList;
        SDKHandler CameraHandler;
        void CameraConnect_Loaded(object sender, RoutedEventArgs e)
        {
            CameraHandler = SDKHandler.Instance;
            CameraHandler.CameraAdded += new SDKHandler.CameraAddedHandler(SDK_CameraAdded);
            RefreshCamera();
        }

        private void connect_Btn_Click(object sender, RoutedEventArgs e)
        {
            if (CameraHandler.CameraSessionOpen)
            {
                CloseSession();
            }

            if (camera_Combobox.SelectedIndex >= 0)
            {
                CameraHandler.OpenSession(CameraHandler.GetCameraList()[camera_Combobox.SelectedIndex]);

                CameraHandler.SetSetting(EDSDK.PropID_SaveTo, (uint)EDSDK.EdsSaveTo.Host);
                string directoryPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\Vtron\\LEDCorrective";
                string path = String.Format("{0}\\{1}", directoryPath, "RemotePhoto");
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                CameraHandler.ImageSaveDirectory = path;

                ModernDialog.ShowMessage("相机连接成功！", "提示", MessageBoxButton.OK);
            }
        }

        private void SDK_CameraAdded()
        {
            RefreshCamera();
        }

        private void RefreshCamera()
        {
            camera_Combobox.Items.Clear();
            CamList = CameraHandler.GetCameraList();
            foreach (Camera cam in CamList) camera_Combobox.Items.Add(cam.Info.szDeviceDescription);
            if (CamList.Count > 0) camera_Combobox.SelectedIndex = 0;
        }

        private void CloseSession()
        {
            CameraHandler.CloseSession();
        }
    }
}
